import gradio as gr
from playwright.sync_api import sync_playwright

def display_file_info(file):
    print(file.name)
    return gr.Image(value=file.name)
    # return [file.name, file.size, file.type]

with gr.Blocks() as demo:
    with gr.Row():
        file_input = gr.File(label="上传文件")
        file_info = gr.Image(label="文件信息")
        button = gr.Button("截图")

        def screenshot():
            with sync_playwright() as p:
                browser = p.chromium.launch()
                page = browser.new_page()
                page.goto('https://www.baidu.com')
                # 截图放到运行程序的目录
                import os
                screenshot_path = os.path.join(os.path.dirname(__file__), 'screenshot.png')
                page.screenshot(full_page=True, path=screenshot_path)
                browser.close()
                return gr.Image(value=screenshot_path)
        button.click(fn=screenshot, outputs=file_info)
    
    file_input.change(fn=display_file_info, inputs=file_input, outputs=file_info)

demo.launch(inbrowser=True)
